﻿// RaidBot by Apoc.

using Styx;
using Styx.Logic.BehaviorTree;
using Styx.Logic.Combat;

using TreeSharp;

namespace RaidBot
{
    public class RaidBot : BotBase
    {
        private byte _oldTps;
        private Composite _root;

        public override string Name { get { return "Raid Bot"; } }
        public override Composite Root { get { return _root; } }

        public override PulseFlags PulseFlags { get { return PulseFlags.Objects | PulseFlags.Lua; } }

        public override void Start()
        {
            _oldTps = TreeRoot.TicksPerSecond;
            TreeRoot.TicksPerSecond = 30;
            _root = new Decorator(
                ret => StyxWoW.Me.Combat && StyxWoW.Me.GotTarget && !StyxWoW.Me.CurrentTarget.IsFriendly,
                new LockSelector(
                    RoutineManager.Current.HealBehavior,
                    RoutineManager.Current.CombatBuffBehavior,
                    RoutineManager.Current.CombatBehavior));
        }


        public override void Stop()
        {
            TreeRoot.TicksPerSecond = _oldTps;
        }


        #region Nested type: LockSelector


        private class LockSelector : PrioritySelector
        {
            public LockSelector(params Composite[] children)
                : base(children)
            {
            }

            public override RunStatus Tick(object context)
            {
                using (new FrameLock())
                {
                    return base.Tick(context);
                }
            }
        }


        #endregion
    }
}